package com.zhixiang.xaec.action.system;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.dom4j.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.google.gson.Gson;
import com.zhixiang.core.command.QueryFilter;
import com.zhixiang.core.entity.TreeObj;
import com.zhixiang.core.util.AppUtil;
import com.zhixiang.core.util.String2Array;
import com.zhixiang.core.web.action.BaseAction;
import com.zhixiang.xaec.entity.system.AppFunction;
import com.zhixiang.xaec.entity.system.AppRole;
import com.zhixiang.xaec.service.system.AppFunctionService;
import com.zhixiang.xaec.service.system.AppRoleService;

@Controller
@Scope("prototype")
public class AppRoleAction extends BaseAction

{

    private static final long serialVersionUID = -5429593630151892200L;

    private AppRole appRole;
    
    @Autowired
    private AppRoleService appRoleService;
    
    @Autowired
    private AppFunctionService appFunctionService;
    
    public String list(){
        QueryFilter localQueryFilter = new QueryFilter(getRequest());
        List<AppRole> list = appRoleService.getAll(localQueryFilter);
        setTotalCounts(localQueryFilter.getPagingBean().getTotalItems());
        setResult(list);
        return SUCCESS;
    }
    
    public String get(){
        String roleId = getRequest().getParameter("roleId");
        
        if(StringUtils.isNotEmpty(roleId)){
            setResult(appRoleService.get(Long.valueOf(roleId)));
        }
        return SUCCESS;
    }
    
    public String delete(){
        try
        {
            String ids = getRequest().getParameter("ids");
            Long[] l = String2Array.stringToLongArray(ids);
            appRoleService.remove(l);
        }
        catch (Exception e)
        {
            setSuccess(false);
            setMsg("删除角色失败");
        }
        return SUCCESS;
    }
    
    public String save(){
        appRoleService.save(appRole);
        return SUCCESS;
    }
    
    public void grantXml()
    {
      Document localDocument = AppUtil.getGrantMenuDocument();
      outXMLString(localDocument.asXML());
    }
    
    public String grant() {
        AppRole localAppRole = appRoleService.get(appRole.getRoleId());
        String str = getRequest().getParameter("rights");
        if (str == null){
        	str = "";
        }
        if (!str.equals(localAppRole.getRights()))
        {
          localAppRole.setRights(str);
          localAppRole.getFunctions().clear();
          String[] arrayOfString = str.split("[,]");
          for (int i = 0; i < arrayOfString.length; i++) {
            if (!arrayOfString[i].startsWith("_")){
            	continue;
            }
            AppFunction localAppFunction = appFunctionService.getByKey(arrayOfString[i]);
            if (localAppFunction != null)
              localAppRole.getFunctions().add(localAppFunction);
          }
          appRoleService.save(localAppRole);
          AppUtil.reloadSecurityDataSource();
        }
        return SUCCESS;
      }
    
    public void getAll(){
        List<AppRole> list = appRoleService.getAll();
        List<TreeObj> treelist = new ArrayList<TreeObj>();
        for (AppRole t : list) {
            TreeObj obj = new TreeObj();
            obj.setId(t.getRoleId().toString());
            obj.setText(t.getRoleName());
            obj.setLeaf(true);
            treelist.add(obj);
        }
        //直接向页面输出字符串
        outJsonString(new Gson().toJson(treelist));
    }

    public AppRole getAppRole()
    {
        return appRole;
    }

    public void setAppRole(AppRole appRole)
    {
        this.appRole = appRole;
    }
    
    
    
}
